/* ====================================================================
 * (#)Header: JaxbTransformer.java
 * (#)Revision: 
 * (#)Date Created: Mar 2, 2007 12:07:22 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.xml;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * <code>JaxbTransformer</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class JaxbTransformer {
	private JAXBContext context;
	private Marshaller marshaller;
	private Unmarshaller unmarshaller;

	private JaxbTransformer() {
	}

	// === public api === //
	public String marshal(Object object) {
		StringWriter writer = new StringWriter();
		try {
			getMarshaller().marshal(object, writer);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return writer.toString();
	}

	@SuppressWarnings("unchecked")
	public <T> T unmarshal(String xml) {
		T object = null;
		try {
			object = (T) getUnmarshaller().unmarshal(new StringReader(xml));
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return object;
	}

	// === public static api === //
	public static String marshal(Object object, Class<?> clazz) {
		return createInstance(clazz).marshal(object);
	}

	@SuppressWarnings("unchecked")
	public static <T> T unmarshal(String xml, Class<T> clazz) {
		return (T) createInstance(clazz).unmarshal(xml);
	}

	// === factory method === //
	public static JaxbTransformer createInstance(Class<?> clazz) {
		JaxbTransformer transformer = new JaxbTransformer();
		try {
			JAXBContext context = JAXBContext.newInstance(clazz);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			transformer.setContext(context);
			transformer.setMarshaller(marshaller);
			transformer.setUnmarshaller(unmarshaller);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
		return transformer;
	}

	// === get/set === //
	public JAXBContext getContext() {
		return context;
	}

	protected void setContext(JAXBContext context) {
		this.context = context;
	}

	public Marshaller getMarshaller() {
		return marshaller;
	}

	protected void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}

	public Unmarshaller getUnmarshaller() {
		return unmarshaller;
	}

	protected void setUnmarshaller(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}
}
